package com.caipiao.crawler.football;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.caipiao.constant.CrawlerConstants;
import com.caipiao.crawler.mapper.LotteryFootballMapper;
import com.caipiao.crawler.model.DaletouResultModel;
import com.caipiao.crawler.model.JingqiucaiResultModel;
import com.caipiao.crawler.model.LotteryFootball;
import com.caipiao.crawler.model.LotteryNumber;
import com.caipiao.crawler.page.BasePageProcessor;
import com.caipiao.util.DBTools;
import net.minidev.json.JSONUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by adminitrator on 2018/7/28.
 */
public class FootballResultPage extends BasePageProcessor {

    private static Log log = LogFactory.getLog(FootballResultPage.class);

    private Site site = Site.me()
            .setRetryTimes(3)
            .setSleepTime(1000)
            .addHeader("Accept","application/json, text/javascript, */*; q=0.01")
            .addHeader("Accept-Encoding", "gzip, deflate")
            .addHeader("Accept-Language","zh-CN,zh;q=0.9")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36");

    public Site getSite() {
        return site;
    }

    @Override
    public void process(Page page) {
        String url = page.getUrl().toString();
        log.info(url);
        if(url.contains("mid")){
            processDetails(page);
        }else if(url.contains("index.do")){
            processMatch(page);
        }else if(url.contains("result_")){
            processData(page);
        }else if(url.contains("match_list")){
            processMatch1(page);
        }else{
            processResultList(page);
        }
    }

    public  void processResultList(Page page){
        log.info("--------FootballResultPage processResultList start ------------");
        page.setSkip(true);
        String pageNum = page.getHtml().xpath("//div[@class='gy021_04']/div/text()").toString();
        int index = pageNum.indexOf("/");
        int pageNo = Integer.parseInt(pageNum.substring(index+1,index+2));
        for(int i=1;i<pageNo+1;i++){
           String targetUrl =  CrawlerConstants.FOOTBALL_RESULT_LIST_URL.replaceAll("pageNo",String.valueOf(i));
            page.addTargetRequest(targetUrl);
        }
        log.info("--------FootballResultPage processResultList end ------------");
    }

    public void processData(Page page) {
        log.info("--------FootballResultPage processData start ------------");
        int i=2;
        List list = new ArrayList();
        while(true) {
            String date = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[1]/text()").toString();
            if(!date.equals("")) {
                String match = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[2]/text()").toString();
                String match_type = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[3]/text()").toString();
                String zhuTeam = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[4]/span[1]/text()").toString();
                String keTeam = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[4]/span[3]/text()").toString();
                String halfScore = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[5]/text()").toString();
                String score = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[6]/text()").toString();
                String status = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[7]/text()").toString();
                String url = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[8]/a/@href").toString();
                int mid = Integer.parseInt(url.substring(url.indexOf("=")+1));
                LotteryFootball model = new LotteryFootball();
                model.setMid(mid);
                model.setMatch(match);
                model.setMatch_type(match_type);
                model.setDate(date);
                model.setCreated_time((int) (System.currentTimeMillis()/1000L));
                model.setFirst_team(zhuTeam);
                model.setSecond_team(keTeam);
                if(score.length()!=0) {
                    model.setFirst_scroe(Integer.parseInt(score.substring(0, score.indexOf(":"))));
                    model.setSecond_score(Integer.parseInt(score.substring(score.indexOf(":") + 1, score.length())));
                }
                if(halfScore.length()!=0) {
                    model.setFirst_half_score(Integer.parseInt(halfScore.substring(0, halfScore.indexOf(":"))));
                    model.setSecond_half_score(Integer.parseInt(halfScore.substring(halfScore.indexOf(":") + 1, halfScore.length())));
                }
                //更新开奖结果
                if("已完成".equals(status)) {
                    model.setStatus(3);
                    list.add(model);
                }
                i++;
            }else{
                break;
            }
        }
        page.putField("modelList", list);
        log.info("--------FootballResultPage processData end ------------");
    }

    public void processDetails(Page page) {
//        page.setSkip(true);  //不需要传到pipeline
        log.info("--------FootballResultPage processDetails start ------------");
        String url = page.getUrl().toString();
        String mid = url.substring(url.length()-6,url.length());
        String wanfa = page.getHtml().xpath("//table[@class='table1 yylMt20']/tbody/tr[3]/td[1]/text()").toString();
        String type = page.getHtml().xpath("//table[@class='table1 yylMt20']/tbody/tr[1]/td[1]/span/b/text()").toString();
        String wanfa1 = page.getHtml().xpath("//table[@class='table1 yylMt20']/tbody/tr[3]/td[2]/text()").toString();
        String wanfa2 = page.getHtml().xpath("//table[@class='table1 yylMt20']/tbody/tr[3]/td[3]/text()").toString();
        String wanfa3 = page.getHtml().xpath("//table[@class='table1 yylMt20']/tbody/tr[3]/td[4]/text()").toString();
        String wanfa4 = page.getHtml().xpath("//table[@class='table1 yylMt20']/tbody/tr[3]/td[5]/text()").toString();
        String wanfa5 = page.getHtml().xpath("//table[@class='table1 yylMt20']/tbody/tr[3]/td[6]/text()").toString();

       Map<String,Object> table1Map = new LinkedHashMap<String, Object>();
       table1Map.put("title1",type);
        table1Map.put("玩法",wanfa);
        table1Map.put("胜平负",wanfa1);
        table1Map.put("让球胜平负",wanfa2);
        table1Map.put("猜比分",wanfa3);
        table1Map.put("总进球数",wanfa4);
        table1Map.put("半全场胜平负",wanfa5);
        String type2 = page.getHtml().xpath("//table[@class='table2 yylMt20']/tbody[1]/tr[1]/td[1]/span/b/text()").toString();
        int i = 1;
        String count="";
        String winlv ="";
        String pinglv="";
        String fulv ="";
        while(true){
             count = page.getHtml().xpath("//table[@class='table2 yylMt20']/tbody[2]/tr["+i+"]/td[1]/text()").toString();
            if(count==null){
                break;
            }else{
                winlv = page.getHtml().xpath("//table[@class='table2 yylMt20']/tbody[2]/tr["+i+"]/td[2]/text()").toString();
                pinglv = page.getHtml().xpath("//table[@class='table2 yylMt20']/tbody[2]/tr["+i+"]/td[3]/text()").toString();
                fulv = page.getHtml().xpath("//table[@class='table2 yylMt20']/tbody[2]/tr["+i+"]/td[4]/text()").toString();
                i++;
            }
        }
        String type2Str = type2+","+winlv+","+pinglv+","+fulv;
        Map<String,Object> table2Map = new LinkedHashMap<String, Object>();
        table2Map.put("title2",type2);
        table2Map.put("胜",winlv);
        table2Map.put("平",pinglv);
        table2Map.put("负",fulv);

        int j = 1;
        String count2="";
        String rang = "";
        String winlv2 ="";
        String pinglv2="";
        String fulv2 ="";
        String type3 = page.getHtml().xpath("//table[@class='table3 yylMt20']/tbody[1]/tr[1]/td[1]/span/b/text()").toString();
        while(true){
            count2 = page.getHtml().xpath("//table[@class='table3 yylMt20']/tbody[2]/tr["+j+"]/td[1]/text()").toString();
            if(count2==null){
                break;
            }else{
                rang = page.getHtml().xpath("//table[@class='table3 yylMt20']/tbody[2]/tr["+j+"]/td[2]/text()").toString();
                winlv2 = page.getHtml().xpath("//table[@class='table3 yylMt20']/tbody[2]/tr["+j+"]/td[3]/text()").toString();
                pinglv2 = page.getHtml().xpath("//table[@class='table3 yylMt20']/tbody[2]/tr["+j+"]/td[4]/text()").toString();
                fulv2 = page.getHtml().xpath("//table[@class='table3 yylMt20']/tbody[2]/tr["+j+"]/td[5]/text()").toString();
                j++;
            }
        }
        Map<String,Object> table3Map = new LinkedHashMap<String, Object>();
        table3Map.put("title3",type3);
        table3Map.put("让球",rang);
        table3Map.put("胜",winlv2);
        table3Map.put("平",pinglv2);
        table3Map.put("负",fulv2);

        String type4 = page.getHtml().xpath("//table[@class='table4 yylMt20']/tbody[1]/tr[1]/td[1]/span/b/text()").toString();
        String context ="";
        int table4Count=1;
        while(true){
            context = page.getHtml().xpath("//table[@class='table4 yylMt20']/tbody[1]/tr["+table4Count+"]").all().toString();
            if(context=="[]"){
                break;
            }else{
                table4Count++;
            }
        }
        Map<String,Object> table4map = new LinkedHashMap<String, Object>();
        String score="";
        String pl="";
        int start = table4Count-6;
        table4map.put("title4",type4);
        for(int k=0;k<3;k++){
            for(int l=1;l<14;l++) {
                int scoreStart = start+2*k;
                int plStart = start+2*k+1;
                score = page.getHtml().xpath("//table[@class='table4 yylMt20']/tbody/tr[" + scoreStart + "]/td["+l+"]/text()").toString();
                pl = page.getHtml().xpath("//table[@class='table4 yylMt20']/tbody/tr[" + plStart + "]/td["+l+"]/text()").toString();
                if(score!=null||pl!=null){
                    table4map.put(score, pl);
                }
            }
        }

        String type5 = page.getHtml().xpath("//table[@class='table5 yylMt20']/tbody[1]/tr[1]/td[1]/span/b/text()").toString();

        int m = 1;
        String count3="";
        String pl1 = "";
        String pl2 ="";
        String pl3="";
        String pl4 ="";
        String pl5 ="";
        String pl6="";
        String pl7 ="";
        String pl8 ="";
        while(true){
            count3 = page.getHtml().xpath("//table[@class='table5 yylMt20']/tbody[2]/tr["+m+"]/td[1]/text()").toString();
            if(count3==null){
                break;
            }else{
                pl1 = page.getHtml().xpath("//table[@class='table5 yylMt20']/tbody[2]/tr["+m+"]/td[2]/text()").toString();
                pl2 = page.getHtml().xpath("//table[@class='table5 yylMt20']/tbody[2]/tr["+m+"]/td[3]/text()").toString();
                pl3 = page.getHtml().xpath("//table[@class='table5 yylMt20']/tbody[2]/tr["+m+"]/td[4]/text()").toString();
                pl4 = page.getHtml().xpath("//table[@class='table5 yylMt20']/tbody[2]/tr["+m+"]/td[5]/text()").toString();
                pl5 = page.getHtml().xpath("//table[@class='table5 yylMt20']/tbody[2]/tr["+m+"]/td[6]/text()").toString();
                pl6 = page.getHtml().xpath("//table[@class='table5 yylMt20']/tbody[2]/tr["+m+"]/td[7]/text()").toString();
                pl7 = page.getHtml().xpath("//table[@class='table5 yylMt20']/tbody[2]/tr["+m+"]/td[8]/text()").toString();
                pl8 = page.getHtml().xpath("//table[@class='table5 yylMt20']/tbody[2]/tr["+m+"]/td[9]/text()").toString();
                m++;
            }
        }
        Map<String,Object> table5Map = new LinkedHashMap<String, Object>();
        table5Map.put("title5",type5);
        table5Map.put("0",pl1);
        table5Map.put("1",pl2);
        table5Map.put("2",pl3);
        table5Map.put("3",pl4);
        table5Map.put("4",pl5);
        table5Map.put("5",pl6);
        table5Map.put("6",pl7);
        table5Map.put("7+",pl8);

        String type6 = page.getHtml().xpath("//table[@class='table6 yylMt20']/tbody[1]/tr[1]/td[1]/span/b/text()").toString();

        int n = 1;
        String count4="";
         pl1 = "";
         pl2 ="";
         pl3="";
         pl4 ="";
         pl5 ="";
         pl6="";
         pl7 ="";
         pl8 ="";
         String pl9="";
        while(true){
            count4 = page.getHtml().xpath("//table[@class='table6 yylMt20']/tbody[2]/tr["+n+"]/td[1]/text()").toString();
            if(count4==null){
                break;
            }else{
                pl1 = page.getHtml().xpath("//table[@class='table6 yylMt20']/tbody[2]/tr["+n+"]/td[2]/text()").toString();
                pl2 = page.getHtml().xpath("//table[@class='table6 yylMt20']/tbody[2]/tr["+n+"]/td[3]/text()").toString();
                pl3 = page.getHtml().xpath("//table[@class='table6 yylMt20']/tbody[2]/tr["+n+"]/td[4]/text()").toString();
                pl4 = page.getHtml().xpath("//table[@class='table6 yylMt20']/tbody[2]/tr["+n+"]/td[5]/text()").toString();
                pl5 = page.getHtml().xpath("//table[@class='table6 yylMt20']/tbody[2]/tr["+n+"]/td[6]/text()").toString();
                pl6 = page.getHtml().xpath("//table[@class='table6 yylMt20']/tbody[2]/tr["+n+"]/td[7]/text()").toString();
                pl7 = page.getHtml().xpath("//table[@class='table6 yylMt20']/tbody[2]/tr["+n+"]/td[8]/text()").toString();
                pl8 = page.getHtml().xpath("//table[@class='table6 yylMt20']/tbody[2]/tr["+n+"]/td[9]/text()").toString();
                pl9 = page.getHtml().xpath("//table[@class='table6 yylMt20']/tbody[2]/tr["+n+"]/td[10]/text()").toString();
                n++;
            }
        }
        Map<String,Object> table6Map = new LinkedHashMap<String, Object>();
        table6Map.put("title6",type6);
        table6Map.put("胜胜",pl1);
        table6Map.put("胜平",pl2);
        table6Map.put("胜负",pl3);
        table6Map.put("平胜",pl4);
        table6Map.put("平平",pl5);
        table6Map.put("平负",pl6);
        table6Map.put("负胜",pl7);
        table6Map.put("负平",pl8);
        table6Map.put("负负",pl9);
        Map<String,Object> map = new LinkedHashMap<String, Object>();
        map.put("detail1",table1Map);
        map.put("detail2",table2Map);
        map.put("detail3",table3Map);
        map.put("detail4",table4map);
        map.put("detail5",table5Map);
        map.put("detail6",table6Map);
        String details = JSON.toJSONString(map);
        System.out.println(details);
        page.putField("details",details);
        page.putField("mid",mid);
        log.info("--------FootballResultPage processDetails end ------------");
    }


    public void processMatch1(Page page) {
        log.info("--------FootballResultPage processMatch1 start ------------");
//        page.setSkip(true);
        int  count = Integer.parseInt(page.getHtml().xpath("//div[@class='result yylFl']/b/text()").toString().trim());
        int j=0;
        int i=2;
        List list = new ArrayList();
        while(j<count) {
            String match = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[1]/text()").toString();
            if(!match.contains("赛事")) {
                String match_type = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[2]/text()").toString();
                String zhuTeam = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[3]/span[1]/text()").toString();
                String keTeam = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[3]/span[3]/text()").toString();
                String date = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[4]/text()").toString();
                String url = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[5]/a/@href").toString();
                String status = page.getHtml().xpath("//div[@class='xxsj']/table/tbody/tr/td/table/tbody/tr["+i+"]/td[6]/text()").toString();
                int mid = Integer.parseInt(url.substring(url.indexOf("=")+1));
//                page.addTargetRequest("http://www.lottery.gov.cn"+url);
                LotteryFootball model = new LotteryFootball();
                model.setMid(mid);
                model.setMatch(match);
                match = match.replaceAll("周一","1");
                match = match.replaceAll("周二","2");
                match =  match.replaceAll("周三","3");
                match = match.replaceAll("周四","4");
                match = match.replaceAll("周五","5");
                match = match.replaceAll("周六","6");
                match = match.replaceAll("周日","7");
                model.setMatch_number(match);
                model.setMatch_type(match_type);
                model.setDate(date);
                SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm" );
                try {
                    Date unixDate = format.parse(date);
                    model.setStart_time(String.valueOf(unixDate.getTime()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                model.setCreated_time((int) (System.currentTimeMillis()/1000L));
                model.setFirst_team(zhuTeam);
                model.setSecond_team(keTeam);
                int statusType = -1;
                if("已开奖".equals(status)){
                    statusType = 3;
                }else if("销售中".equals(status)){
                    statusType = 1;
                }else if("销售暂停".equals(status)){
                    statusType= 0;
                }else if("已结束 ".equals(status)){
                    statusType=2;
                }
                model.setStatus(statusType);
                list.add(model);
//                page.putField("model", model);
                j++;
            }
            i++;
        }
        page.putField("modelList",list);
        log.info("--------FootballResultPage processMatch1 end ------------");
    }
    public void processMatch(Page page) {
//        page.setSkip(true);
        log.info("--------FootballResultPage processMatch start ------------");
        List list = new ArrayList();
        String data = page.getJson().get();
        JSONObject obj = JSON.parseObject(data);
        JSONObject fbList = obj.getJSONObject("fbList");
        JSONArray oneDayList = fbList.getJSONArray("oneDayList");
        for(int i=0;i<oneDayList.size();i++){
            JSONObject dayList = (JSONObject) oneDayList.get(i);
            JSONArray matchList = dayList.getJSONArray("ballMatchList");
            for(int j=0;j<matchList.size();j++){
                JSONObject match = (JSONObject) matchList.get(j);
                LotteryFootball model = new LotteryFootball();
                model.setMid(Integer.parseInt(match.getString("match_id")));
                model.setMatch(match.getString("match_week")+match.getString("line_num"));
                String week = match.getString("match_week");
                if("周一".equals(week)){
                    week = "1";
                }else if("周二".equals(week)){
                    week = "2";
                }else if("周三".equals(week)){
                    week = "3";
                }else if("周四".equals(week)){
                    week = "4";
                }else if("周五".equals(week)){
                    week = "5";
                }else if("周六".equals(week)){
                    week = "6";
                }else if("周日".equals(week)){
                    week = "7";
                }
                model.setMatch_number(week+match.getString("line_num"));
                model.setMatch_type(match.getString("league_name"));
                model.setDate(match.getString("match_time"));
                model.setFirst_team(match.getString("home_team_name"));
                model.setSecond_team(match.getString("guest_team_name"));
                String status = match.getString("match_status");
                int statusType = -1;
                if("已开奖".equals(status)){
                    statusType = 3;
                }else if("销售中".equals(status)){
                    statusType = 1;
                }else if("销售暂停".equals(status)){
                    statusType= 0;
                }else if("已结束 ".equals(status)){
                    statusType=2;
                }
                model.setStatus(statusType);
                model.setCreated_time((int) (System.currentTimeMillis()/1000L));
                list.add(model);
            }
        }
        page.putField("modelList",list);
        log.info("--------FootballResultPage processMatch end ------------");
    }
}
